<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">

        <!-- 引入子组件 -->
        <tree-tools :tree-node="company" :is-root="true" />

        <hr>

        <el-tree
          :default-expand-all="true"
          :data="departs"
          :props="{ label: 'name' }"
        >
          <!-- 引入子组件 -->
          <tree-tools slot-scope="{ data }" :tree-node="data" style="width: 100%" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />

        </el-tree>

      </el-card>

      <!-- 弹出层子组件 -->
      <add-dept ref="addDialog" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import AddDept from './components/add-dept.vue'
export default {
  components: { treeTools, AddDept },
  data() {
    return {
      company: {
        name: '江苏传智播客教育科技股份有限公司',
        manager: '负责人',
        id: ''
      },
      departs: [], // 组织架构
      showDialog: false, // 显示窗体
      node: {} // 操作的节点
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    // 获取组织架构
    async getDepartments() {
      const res = await getDepartments()
      console.log(res)
      this.company = { name: res.companyName, manager: '负责人', id: '' }
      const data = await tranListToTreeData(res.depts, '')
      console.log('-------------------')
      console.dir(data)
      this.departs = data // 需要将其转化成树形结构
    },
    // 添加部门节点
    addDepts(node) {
      this.showDialog = true // 显示弹层
      this.node = node // 赋值操作的节点
    },
    // 编辑部门节点
    async editDepts(node) {
      // 其实编辑和新增获取点击的数据和弹出弹窗处理是一样
      // 唯一的区别是, 如果编辑需要获取旧数据进行回显
      // 只有父组件这里才能知道需要回显数据
      // 所以在这调用子组件的回显方法
      await this.$refs.addDialog.getDepartmentDetail(node.id)
      this.showDialog = true // 显示弹层
      this.node = node // 赋值操作的节点
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px  140px;
  font-size:14px;

}
</style>
